home *** CD-ROM | disk | FTP | other *** search
/ Software of the Month Club 2000 October / Software of the Month - Ultimate Collection Shareware 277.iso / pc / PROGRAMS / UTILITY / WINLINUX / DATA1.CAB / programs_-_Netscape / MOVEMAIL.{_9 / MOVEMAIL.C < prev    next >
C/C++ Source or Header  |  1999-09-17  |  19KB  |  857 lines

  1. /* movemail foo bar -- move file foo to file bar,
  2.    locking file foo the way /bin/mail respects.
  3.    Copyright (C) 1986, 1992, 1993, 1994 Free Software Foundation, Inc.
  4.  
  5. This file is part of GNU Emacs.
  6.  
  7. GNU Emacs is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2, or (at your option)
  10. any later version.
  11.  
  12. GNU Emacs is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with GNU Emacs; see the file COPYING.  If not, write to
  19. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  20.  
  21. /* Extracted from XEmacs 19.13, which said: "synched up with: FSF 19.28." */
  22.  
  23. /* Important notice: defining MAIL_USE_FLOCK or MAIL_USE_LOCKF *will
  24.    cause loss of mail* if you do it on a system that does not normally
  25.    use flock as its way of interlocking access to inbox files.  The
  26.    setting of MAIL_USE_FLOCK and MAIL_USE_LOCKF *must agree* with the
  27.    system's own conventions.  It is not a choice that is up to you.
  28.  
  29.    So, if your system uses lock files rather than flock, then the only way
  30.    you can get proper operation is to enable movemail to write lockfiles there.
  31.    This means you must either give that directory access modes
  32.    that permit everyone to write lockfiles in it, or you must make movemail
  33.    a setuid or setgid program.  */
  34.  
  35. /*
  36.  * Modified January, 1986 by Michael R. Gretzinger (Project Athena)
  37.  *
  38.  * Added POP (Post Office Protocol) service.  When compiled -DPOP
  39.  * movemail will accept input filename arguments of the form
  40.  * "po:username".  This will cause movemail to open a connection to
  41.  * a pop server running on $MAILHOST (environment variable).  Movemail
  42.  * must be setuid to root in order to work with POP.
  43.  * 
  44.  * New module: popmail.c
  45.  * Modified routines:
  46.  *    main - added code within #ifdef MAIL_USE_POP; added setuid (getuid ())
  47.  *        after POP code. 
  48.  * New routines in movemail.c:
  49.  *    get_errmsg - return pointer to system error message
  50.  *
  51.  */
  52.  
  53. #define NO_SHORTNAMES   /* Tell config not to load remap.h */
  54. #ifndef NETSCAPE
  55. #include <../src/config.h>
  56. #endif /* !NETSCAPE */
  57. #include <stdlib.h>
  58. #include <unistd.h>
  59. #include <time.h> /* for time() */
  60. #include <stdio.h> /* for printf() */
  61. #include <string.h> /* strcpy() */
  62.  
  63. #include <sys/types.h>
  64. #include <sys/stat.h>
  65. #include <sys/file.h>
  66. #include <errno.h>
  67. #ifndef NETSCAPE
  68. #include <../src/syswait.h>
  69. #endif /* !NETSCAPE */
  70.  
  71. #ifdef NETSCAPE
  72.  
  73. #ifndef WAITTYPE
  74. #define WAITTYPE int
  75. #endif /* !WAITTYPE */
  76.  
  77. #ifndef WIFEXITED
  78. #include <sys/wait.h>
  79. #endif /* !WIFEXITED */
  80.  
  81. #ifndef WRETCODE
  82. #define WRETCODE(w) WEXITSTATUS(w)
  83. #endif /* !WRETCODE */
  84.  
  85. #endif /* NETSCAPE */
  86.  
  87. #ifdef MSDOS
  88. #undef access
  89. #endif /* MSDOS */
  90.  
  91. #ifdef USG
  92. #include <fcntl.h>
  93. #include <unistd.h>
  94. #if defined (sun)
  95. #include <stdlib.h>
  96. #endif /* sun */
  97. #ifndef F_OK
  98. #define F_OK 0
  99. #define X_OK 1
  100. #define W_OK 2
  101. #define R_OK 4
  102. #endif
  103. #endif /* USG */
  104.  
  105. #ifdef HAVE_UNISTD_H
  106. #include <unistd.h>
  107. #endif
  108.  
  109. #ifdef XENIX
  110. #include <sys/locking.h>
  111. #endif
  112.  
  113. #ifdef MAIL_USE_LOCKF
  114. #define MAIL_USE_SYSTEM_LOCK
  115. #endif
  116.  
  117. #ifdef MAIL_USE_FLOCK
  118. #define MAIL_USE_SYSTEM_LOCK
  119. #endif
  120.  
  121. #ifdef MAIL_USE_MMDF
  122. extern int lk_open (), lk_close ();
  123. #endif
  124.  
  125. /* Cancel substitutions made by config.h for Emacs.  */
  126. #undef open
  127. #undef read
  128. #undef write
  129. #undef close
  130.  
  131. static char *concat (char *s1, char *s2, char *s3);
  132. static void *xmalloc (unsigned int size);
  133. #ifndef errno
  134. extern int errno;
  135. #endif
  136.  
  137. static void error (char *s1, char *s2, char *s3);
  138. static void fatal (char *s1, char *s2);
  139. static void pfatal_with_name (char *name);
  140. static void pfatal_and_delete (char *name);
  141.  
  142. #if defined(linux) && defined(__GLIBC__)
  143.  
  144. #if !defined(HAVE_STRERROR)
  145. #define HAVE_STRERROR 1
  146. #endif /* HAVE_STRERROR */
  147.  
  148. #endif /* LINUX && __GLIBC__ */
  149.  
  150. #ifndef HAVE_STRERROR
  151. char *strerror (int);
  152. #endif
  153.  
  154. /* Nonzero means this is name of a lock file to delete on fatal error.  */
  155. char *delete_lockname;
  156.  
  157. void
  158. main (argc, argv)
  159.      int argc;
  160.      char **argv;
  161. {
  162.   char *inname, *outname;
  163.   int indesc, outdesc;
  164.   int nread;
  165.   WAITTYPE status;
  166.  
  167. #ifndef MAIL_USE_SYSTEM_LOCK
  168.   struct stat st;
  169.   long now;
  170.   int tem;
  171.   char *lockname, *p;
  172.   char *tempname;
  173.   int desc;
  174. #endif /* not MAIL_USE_SYSTEM_LOCK */
  175.  
  176.   delete_lockname = 0;
  177.  
  178.   if (argc < 3)
  179.     fatal ("two arguments required", "");
  180.  
  181.   inname = argv[1];
  182.   outname = argv[2];
  183.  
  184. #ifdef MAIL_USE_MMDF
  185.   mmdf_init (argv[0]);
  186. #endif
  187.  
  188.   /* Check access to output file.  */
  189.   if (access (outname, F_OK) == 0 && access (outname, W_OK) != 0)
  190.     pfatal_with_name (outname);
  191.  
  192.   /* Also check that outname's directory is writeable to the real uid.  */
  193.   {
  194.     char *buf = (char *) xmalloc (strlen (outname) + 1);
  195.     char *p;
  196.     strcpy (buf, outname);
  197.     p = buf + strlen (buf);
  198.     while (p > buf && p[-1] != '/')
  199.       *--p = 0;
  200.     if (p == buf)
  201.       *p++ = '.';
  202.     if (access (buf, W_OK) != 0)
  203.       pfatal_with_name (buf);
  204.     free (buf);
  205.   }
  206.  
  207. #ifdef MAIL_USE_POP
  208.   if (!memcmp (inname, "po:", 3))
  209.     {
  210.       int status; char *user;
  211.  
  212.       for (user = &inname[strlen (inname) - 1]; user >= inname; user--)
  213.     if (*user == ':')
  214.       break;
  215.  
  216.       status = popmail (user, outname);
  217.       exit (status);
  218.     }
  219.  
  220.   setuid (getuid ());
  221. #endif /* MAIL_USE_POP */
  222.  
  223.   /* Check access to input file.  */
  224.   if (access (inname, R_OK | W_OK) != 0)
  225.     pfatal_with_name (inname);
  226.  
  227. #ifndef MAIL_USE_MMDF
  228. #ifndef MAIL_USE_SYSTEM_LOCK
  229.   /* Use a lock file named /usr/spool/mail/$USER.lock:
  230.      If it exists, the mail file is locked.  */
  231.   /* Note: this locking mechanism is *required* by the mailer
  232.      (on systems which use it) to prevent loss of mail.
  233.  
  234.      On systems that use a lock file, extracting the mail without locking
  235.      WILL occasionally cause loss of mail due to timing errors!
  236.  
  237.      So, if creation of the lock file fails
  238.      due to access permission on /usr/spool/mail,
  239.      you simply MUST change the permission
  240.      and/or make movemail a setgid program
  241.      so it can create lock files properly.
  242.  
  243.      You might also wish to verify that your system is one
  244.      which uses lock files for this purpose.  Some systems use other methods.
  245.  
  246.      If your system uses the `flock' system call for mail locking,
  247.      define MAIL_USE_SYSTEM_LOCK in config.h or the s-*.h file
  248.      and recompile movemail.  If the s- file for your system
  249.      should define MAIL_USE_SYSTEM_LOCK but does not, send a bug report
  250.      to bug-gnu-emacs@prep.ai.mit.edu so we can fix it.  */
  251.  
  252.   lockname = concat (inname, ".lock", "");
  253.   tempname = (char *) xmalloc (strlen (inname) + strlen ("EXXXXXX") + 1);
  254.   strcpy (tempname, inname);
  255.   p = tempname + strlen (tempname);
  256.   while (p != tempname && p[-1] != '/')
  257.     p--;
  258.   *p = 0;
  259.   strcpy (p, "EXXXXXX");
  260.   mktemp (tempname);
  261.   unlink (tempname);
  262.  
  263.   while (1)
  264.     {
  265.       /* Create the lock file, but not under the lock file name.  */
  266.       /* Give up if cannot do that.  */
  267.       desc = open (tempname, O_WRONLY | O_CREAT | O_EXCL, 0666);
  268.       if (desc < 0)
  269.         pfatal_with_name ("lock file--see source file lib-src/movemail.c");
  270.       close (desc);
  271.  
  272.       tem = link (tempname, lockname);
  273.       unlink (tempname);
  274.       if (tem >= 0)
  275.     break;
  276.       sleep (1);
  277.  
  278.       /* If lock file is a minute old, unlock it.  */
  279.       if (stat (lockname, &st) >= 0)
  280.     {
  281.       now = time (0);
  282.       if (st.st_ctime < now - 60)
  283.             unlink (lockname);
  284.     }
  285.     }
  286.  
  287.   delete_lockname = lockname;
  288. #endif /* not MAIL_USE_SYSTEM_LOCK */
  289. #endif /* not MAIL_USE_MMDF */
  290.  
  291.   if (fork () == 0)
  292.     {
  293.       setuid (getuid ());
  294.  
  295. #ifndef MAIL_USE_MMDF
  296. #ifdef MAIL_USE_SYSTEM_LOCK
  297.       indesc = open (inname, O_RDWR);
  298. #else  /* if not MAIL_USE_SYSTEM_LOCK */
  299.       indesc = open (inname, O_RDONLY);
  300. #endif /* not MAIL_USE_SYSTEM_LOCK */
  301. #else  /* MAIL_USE_MMDF */
  302.       indesc = lk_open (inname, O_RDONLY, 0, 0, 10);
  303. #endif /* MAIL_USE_MMDF */
  304.  
  305.       if (indesc < 0)
  306.     pfatal_with_name (inname);
  307.  
  308. #if defined (BSD) || defined (XENIX)
  309.       /* In case movemail is setuid to root, make sure the user can
  310.      read the output file.  */
  311.       /* This is desirable for all systems
  312.      but I don't want to assume all have the umask system call */
  313.       umask (umask (0) & 0333);
  314. #endif /* BSD or Xenix */
  315.       outdesc = open (outname, O_WRONLY | O_CREAT | O_EXCL, 0666);
  316.       if (outdesc < 0)
  317.     pfatal_with_name (outname);
  318. #ifdef MAIL_USE_SYSTEM_LOCK
  319. #ifdef MAIL_USE_LOCKF
  320.       if (lockf (indesc, F_LOCK, 0) < 0) pfatal_with_name (inname);
  321. #else /* not MAIL_USE_LOCKF */
  322. #ifdef XENIX
  323.       if (locking (indesc, LK_RLCK, 0L) < 0) pfatal_with_name (inname);
  324. #else
  325.       if (flock (indesc, LOCK_EX) < 0) pfatal_with_name (inname);
  326. #endif
  327. #endif /* not MAIL_USE_LOCKF */
  328. #endif /* MAIL_USE_SYSTEM_LOCK */
  329.  
  330.       {
  331.     char buf[1024];
  332.  
  333.     while (1)
  334.       {
  335.         nread = read (indesc, buf, sizeof buf);
  336.         if (nread != write (outdesc, buf, nread))
  337.           {
  338.         int saved_errno = errno;
  339.         unlink (outname);
  340.         errno = saved_errno;
  341.         pfatal_with_name (outname);
  342.           }
  343.         if (nread < sizeof buf)
  344.           break;
  345.       }
  346.       }
  347.  
  348. #ifdef BSD
  349.       if (fsync (outdesc) < 0)
  350.     pfatal_and_delete (outname);
  351. #endif
  352.  
  353.       /* Check to make sure no errors before we zap the inbox.  */
  354.       if (close (outdesc) != 0)
  355.     pfatal_and_delete (outname);
  356.  
  357. #ifdef MAIL_USE_SYSTEM_LOCK
  358. #if defined (STRIDE) || defined (XENIX)
  359.       /* Stride, xenix have file locking, but no ftruncate.  This mess will do. */
  360.       close (open (inname, O_CREAT | O_TRUNC | O_RDWR, 0666));
  361. #else
  362.       ftruncate (indesc, 0L);
  363. #endif /* STRIDE or XENIX */
  364. #endif /* MAIL_USE_SYSTEM_LOCK */
  365.  
  366. #ifdef MAIL_USE_MMDF
  367.       lk_close (indesc, 0, 0, 0);
  368. #else
  369.       close (indesc);
  370. #endif
  371.  
  372. #ifndef MAIL_USE_SYSTEM_LOCK
  373.       /* Delete the input file; if we can't, at least get rid of its
  374.      contents.  */
  375. #ifdef MAIL_UNLINK_SPOOL
  376.       /* This is generally bad to do, because it destroys the permissions
  377.      that were set on the file.  Better to just empty the file.  */
  378.       if (unlink (inname) < 0 && errno != ENOENT)
  379. #endif /* MAIL_UNLINK_SPOOL */
  380.     creat (inname, 0600);
  381. #endif /* not MAIL_USE_SYSTEM_LOCK */
  382.  
  383.       exit (0);
  384.     }
  385.  
  386.   wait (&status);
  387.   if (!WIFEXITED (status))
  388.     exit (1);
  389.   else if (WRETCODE (status) != 0)
  390.     exit (WRETCODE (status));
  391.  
  392. #if !defined (MAIL_USE_MMDF) && !defined (MAIL_USE_SYSTEM_LOCK)
  393.   unlink (lockname);
  394. #endif /* not MAIL_USE_MMDF and not MAIL_USE_SYSTEM_LOCK */
  395.   exit (0);
  396. }
  397.  
  398. /* Print error message and exit.  */
  399.  
  400. static void
  401. fatal (s1, s2)
  402.      char *s1, *s2;
  403. {
  404.   if (delete_lockname)
  405.     unlink (delete_lockname);
  406.   error (s1, s2, "");
  407.   exit (1);
  408. }
  409.  
  410. /* Print error message.  `s1' is printf control string, `s2' is arg for it. */
  411.  
  412. static void
  413. error (s1, s2, s3)
  414.      char *s1, *s2, *s3;
  415. {
  416.   printf ("movemail: ");
  417.   printf (s1, s2, s3);
  418.   printf ("\n");
  419. }
  420.  
  421. static void
  422. pfatal_with_name (name)
  423.      char *name;
  424. {
  425.   char *s;
  426.  
  427.   s = concat ("", strerror (errno), " for %s");
  428.   fatal (s, name);
  429. }
  430.  
  431. static void
  432. pfatal_and_delete (name)
  433.      char *name;
  434. {
  435.   char *s;
  436.  
  437.   s = concat ("", strerror (errno), " for %s");
  438.   unlink (name);
  439.   fatal (s, name);
  440. }
  441.  
  442. /* Return a newly-allocated string whose contents concatenate those of s1, s2, s3.  */
  443.  
  444. static char *
  445. concat (s1, s2, s3)
  446.      char *s1, *s2, *s3;
  447. {
  448.   int len1 = strlen (s1), len2 = strlen (s2), len3 = strlen (s3);
  449.   char *result = (char *) xmalloc (len1 + len2 + len3 + 1);
  450.  
  451.   strcpy (result, s1);
  452.   strcpy (result + len1, s2);
  453.   strcpy (result + len1 + len2, s3);
  454.   *(result + len1 + len2 + len3) = 0;
  455.  
  456.   return result;
  457. }
  458.  
  459. /* Like malloc but get fatal error if memory is exhausted.  */
  460.  
  461. static void *
  462. xmalloc (size)
  463.      unsigned int size;
  464. {
  465.   void *result = (void *) malloc (size);
  466.   if (!result)
  467.     fatal ("virtual memory exhausted", (char *) 0);
  468.   return result;
  469. }
  470.  
  471. /* This is the guts of the interface to the Post Office Protocol.  */
  472.  
  473. #ifdef MAIL_USE_POP
  474.  
  475. #include <sys/socket.h>
  476. #include <netinet/in.h>
  477. #include <netdb.h>
  478. #include <stdio.h>
  479. #include <pwd.h>
  480.  
  481. #ifdef USG
  482. #include <fcntl.h>
  483. /* Cancel substitutions made by config.h for Emacs.  */
  484. #undef open
  485. #undef read
  486. #undef write
  487. #undef close
  488. #endif /* USG */
  489.  
  490. #define NOTOK (-1)
  491. #define OK 0
  492. #define DONE 1
  493.  
  494. char *progname;
  495. FILE *sfi;
  496. FILE *sfo;
  497. char Errmsg[80];
  498.  
  499. static int debug = 0;
  500.  
  501. char *get_errmsg ();
  502. char *getenv ();
  503. int mbx_write ();
  504.  
  505. int
  506. popmail (user, outfile)
  507.      char *user;
  508.      char *outfile;
  509. {
  510.   char *host;
  511.   int nmsgs, nbytes;
  512.   char response[128];
  513.   register int i;
  514.   int mbfi;
  515.   FILE *mbf;
  516.   struct passwd *pw = (struct passwd *) getpwuid (getuid ());
  517.   if (pw == NULL)
  518.     fatal ("cannot determine user name");
  519.  
  520.   host = getenv ("MAILHOST");
  521.   if (host == NULL) 
  522.     {
  523.       fatal ("no MAILHOST defined");
  524.     }
  525.  
  526.   if (pop_init (host) == NOTOK) 
  527.     {
  528.       fatal (Errmsg);
  529.     }
  530.  
  531.   if (getline (response, sizeof response, sfi) != OK) 
  532.     {
  533.       fatal (response);
  534.     }
  535.  
  536.   if (pop_command ("USER %s", user) == NOTOK 
  537.       || pop_command ("RPOP %s", pw->pw_name) == NOTOK) 
  538.     {
  539.       pop_command ("QUIT");
  540.       fatal (Errmsg);
  541.     }
  542.  
  543.   if (pop_stat (&nmsgs, &nbytes) == NOTOK) 
  544.     {
  545.       pop_command ("QUIT");
  546.       fatal (Errmsg);
  547.     }
  548.  
  549.   if (!nmsgs)
  550.   {
  551.     pop_command ("QUIT");
  552.     return 0;
  553.   }
  554.  
  555.   mbfi = open (outfile, O_WRONLY | O_CREAT | O_EXCL, 0666);
  556.   if (mbfi < 0)
  557.   {
  558.     pop_command ("QUIT");
  559.     pfatal_and_delete (outfile);
  560.   }
  561.   fchown (mbfi, getuid (), -1);
  562.  
  563.   if ((mbf = fdopen (mbfi, "w")) == NULL)
  564.     {
  565.       pop_command ("QUIT");
  566.       pfatal_and_delete (outfile);
  567.     }
  568.  
  569.   for (i = 1; i <= nmsgs; i++) 
  570.     {
  571.       mbx_delimit_begin (mbf);
  572.       if (pop_retr (i, mbx_write, mbf) != OK) 
  573.         {
  574.           pop_command ("QUIT");
  575.           close (mbfi);
  576.       unlink (outfile);
  577.       fatal (Errmsg);
  578.         }
  579.       mbx_delimit_end (mbf);
  580.       fflush (mbf);
  581.     }
  582.  
  583.   if (fsync (mbfi) < 0)
  584.     {
  585.       pop_command ("QUIT");
  586.       pfatal_and_delete (outfile);
  587.     }
  588.  
  589.   if (close (mbfi) == -1)
  590.     {
  591.       pop_command ("QUIT");
  592.       pfatal_and_delete (outfile);
  593.     }
  594.  
  595.   for (i = 1; i <= nmsgs; i++) 
  596.     {
  597.       if (pop_command ("DELE %d", i) == NOTOK) 
  598.         {
  599.       /* Better to ignore this failure.  */
  600.         }
  601.     }
  602.  
  603.   pop_command ("QUIT");
  604.   return (0);
  605. }
  606.  
  607. int
  608. pop_init (host)
  609.      char *host;
  610. {
  611.   register struct hostent *hp;
  612.   register struct servent *sp;
  613.   int lport = IPPORT_RESERVED - 1;
  614.   struct sockaddr_in sin;
  615.   register int s;
  616.  
  617.   hp = gethostbyname (host);
  618.   if (hp == NULL) 
  619.     {
  620.       sprintf (Errmsg, "MAILHOST unknown: %s", host);
  621.          /* Reviewed 4.51 safe use of sprintf */
  622.       return NOTOK;
  623.     }
  624.  
  625.   sp = getservbyname ("pop", "tcp");
  626.   if (sp == 0) 
  627.     {
  628.       strcpy (Errmsg, "tcp/pop: unknown service");
  629.       return NOTOK;
  630.     }
  631.  
  632.   sin.sin_family = hp->h_addrtype;
  633.   memcpy ((char *)&sin.sin_addr, hp->h_addr, hp->h_length);
  634.   sin.sin_port = sp->s_port;
  635.   s = rresvport (&lport);
  636.   if (s < 0) 
  637.     {
  638.       sprintf (Errmsg, "error creating socket: %s", get_errmsg ());
  639.          /* Reviewed 4.51 safe use of sprintf */
  640.       return NOTOK;
  641.     }
  642.  
  643.   if (connect (s, (char *)&sin, sizeof sin) < 0) 
  644.     {
  645.       sprintf (Errmsg, "error during connect: %s", get_errmsg ());
  646.          /* Reviewed 4.51 safe use of sprintf */
  647.       close (s);
  648.       return NOTOK;
  649.     }
  650.  
  651.   sfi = fdopen (s, "r");
  652.   sfo = fdopen (s, "w");
  653.   if (sfi == NULL || sfo == NULL) 
  654.     {
  655.       sprintf (Errmsg, "error in fdopen: %s", get_errmsg ());
  656.          /* Reviewed 4.51 safe use of sprintf */
  657.       close (s);
  658.       return NOTOK;
  659.     }
  660.  
  661.   return OK;
  662. }
  663.  
  664. int
  665. pop_command (fmt, a, b, c, d)
  666.      char *fmt;
  667. {
  668.   char buf[128];
  669.   char errmsg[64];
  670.  
  671.   sprintf (buf, fmt, a, b, c, d);   /* Reviewed 4.51 safe use of sprintf */
  672.  
  673.   if (debug) fprintf (stderr, "---> %s\n", buf);
  674.   if (putline (buf, Errmsg, sfo) == NOTOK) return NOTOK;
  675.  
  676.   if (getline (buf, sizeof buf, sfi) != OK) 
  677.     {
  678.       strcpy (Errmsg, buf);
  679.       return NOTOK;
  680.     }
  681.  
  682.   if (debug) fprintf (stderr, "<--- %s\n", buf);
  683.   if (*buf != '+') 
  684.     {
  685.       strcpy (Errmsg, buf);
  686.       return NOTOK;
  687.     } 
  688.   else 
  689.     {
  690.       return OK;
  691.     }
  692. }
  693.  
  694.     
  695. pop_stat (nmsgs, nbytes)
  696.      int *nmsgs, *nbytes;
  697. {
  698.   char buf[128];
  699.  
  700.   if (debug) fprintf (stderr, "---> STAT\n");
  701.   if (putline ("STAT", Errmsg, sfo) == NOTOK)
  702.     return NOTOK;
  703.  
  704.   if (getline (buf, sizeof buf, sfi) != OK) 
  705.     {
  706.       strcpy (Errmsg, buf);
  707.       return NOTOK;
  708.     }
  709.  
  710.   if (debug) fprintf (stderr, "<--- %s\n", buf);
  711.   if (*buf != '+') 
  712.     {
  713.       strcpy (Errmsg, buf);
  714.       return NOTOK;
  715.     } 
  716.   else 
  717.     {
  718.       sscanf (buf, "+OK %d %d", nmsgs, nbytes);
  719.       return OK;
  720.     }
  721. }
  722.  
  723. pop_retr (msgno, action, arg)
  724.      int (*action)();
  725. {
  726.   char buf[128];
  727.  
  728.   sprintf (buf, "RETR %d", msgno);   /* Reviewed 4.51 safe use of sprintf */
  729.   if (debug) fprintf (stderr, "%s\n", buf);
  730.   if (putline (buf, Errmsg, sfo) == NOTOK) return NOTOK;
  731.  
  732.   if (getline (buf, sizeof buf, sfi) != OK) 
  733.     {
  734.       strcpy (Errmsg, buf);
  735.       return NOTOK;
  736.     }
  737.  
  738.   while (1) 
  739.     {
  740.       switch (multiline (buf, sizeof buf, sfi)) 
  741.         {
  742.         case OK:
  743.           (*action)(buf, arg);
  744.           break;
  745.         case DONE:
  746.           return OK;
  747.         case NOTOK:
  748.           strcpy (Errmsg, buf);
  749.           return NOTOK;
  750.         }
  751.     }
  752. }
  753.  
  754. getline (buf, n, f)
  755.      char *buf;
  756.      register int n;
  757.      FILE *f;
  758. {
  759.   register char *p;
  760.   int c;
  761.  
  762.   p = buf;
  763.   while (--n > 0 && (c = fgetc (f)) != EOF)
  764.     if ((*p++ = c) == '\n') break;
  765.  
  766.   if (ferror (f)) 
  767.   {
  768.     strcpy (buf, "error on connection");
  769.     return NOTOK;
  770.   }
  771.  
  772.   if (c == EOF && p == buf) 
  773.   {
  774.     strcpy (buf, "connection closed by foreign host");
  775.     return DONE;
  776.   }
  777.  
  778.   *p = NULL;
  779.   if (*--p == '\n') *p = NULL;
  780.   if (*--p == '\r') *p = NULL;
  781.   return OK;
  782. }
  783.  
  784. multiline (buf, n, f)
  785.      char *buf;
  786.      register int n;
  787.      FILE *f;
  788. {
  789.   if (getline (buf, n, f) != OK) return NOTOK;
  790.   if (*buf == '.') 
  791.     {
  792.       if (*(buf+1) == NULL) 
  793.     return DONE;
  794.       else 
  795.         strcpy (buf, buf+1);
  796.     }
  797.   return OK;
  798. }
  799.  
  800. char *
  801. get_errmsg ()
  802. {
  803.   return strerror (errno);
  804. }
  805.  
  806. putline (buf, err, f)
  807.      char *buf;
  808.      char *err;
  809.      FILE *f;
  810. {
  811.   fprintf (f, "%s\r\n", buf);
  812.   fflush (f);
  813.   if (ferror (f)) 
  814.     {
  815.       strcpy (err, "lost connection");
  816.       return NOTOK;
  817.     }
  818.   return OK;
  819. }
  820.  
  821. mbx_write (line, mbf)
  822.      char *line;
  823.      FILE *mbf;
  824. {
  825.   fputs (line, mbf);
  826.   fputc (0x0a, mbf);
  827. }
  828.  
  829. mbx_delimit_begin (mbf)
  830.      FILE *mbf;
  831. {
  832.   fputs ("\f\n0, unseen,,\n", mbf);
  833. }
  834.  
  835. mbx_delimit_end (mbf)
  836.      FILE *mbf;
  837. {
  838.   putc ('\037', mbf);
  839. }
  840.  
  841. #endif /* MAIL_USE_POP */
  842.  
  843. #ifndef HAVE_STRERROR
  844. char *
  845. strerror (errnum)
  846.      int errnum;
  847. {
  848.   extern char *sys_errlist[];
  849.   extern int sys_nerr;
  850.  
  851.   if (errnum >= 0 && errnum < sys_nerr)
  852.     return sys_errlist[errnum];
  853.   return (char *) "Unknown error";
  854. }
  855.  
  856. #endif /* ! HAVE_STRERROR */
  857.